
package com.funo.government.info.utils;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

/**
 * 业务接口调用辅助类
 * 
 * @author 王洁
 */
public abstract class BusinessResolver {

    private static final String TAG = "BusinessResolver";

    private static final String PRAM_RESULT = "reqcode";

    private static final String PRAM_MSG = "reqmsg";

    private static final String PRAM_DATA = "data";

    // 正式地址
    protected static final String DOMAIN = "http://220.162.144.198:8087/sf_lc";

    // 测试地址
    // protected static final String DOMAIN = "http://218.85.55.145:9980/sf_lc";

    /**
     * 解析json字符串，判断是否正确返回结果，正确则解析出data属性中的内容，否则抛出BusinessException异常
     * 
     * @param json
     * @return
     * @throws BusinessException
     */
    private static String resolverData(String json) throws BusinessException {

        if (TextUtils.isEmpty(json)) {
            throw new BusinessException(BusinessException.CODE_ILLEGAL_RETURN);
        }

        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(json);
        } catch (JSONException e) {
            throw new BusinessException(BusinessException.CODE_ILLEGAL_RETURN);
        }
        String result = jsonObject.optString(PRAM_RESULT);
        if (TextUtils.isEmpty(result)) {
            throw new BusinessException(BusinessException.CODE_ILLEGAL_RETURN);
        }
        if ("0000".equals(result)) {
            return jsonObject.optString(PRAM_DATA);
        } else {
            String msg = jsonObject.optString(PRAM_MSG);
            LogUtils.e(TAG, "result:" + result + ";msg:" + msg);
            throw new BusinessException(result, msg, json);
        }
    }

    /**
     * 使用GET方式获取数据
     * 
     * @param url
     * @return
     * @throws BusinessException
     */
    static String getData(String url) throws BusinessException {
        LogUtils.i(TAG, "url:" + url);
        try {
            String json = NetUtils.doGetString(url);
            LogUtils.i(TAG, "result:" + json);
            return resolverData(json);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(BusinessException.CODE_UNREACH_SERVER);
        }
    }

    /**
     * 使用GET方式获取数据
     * 
     * @param url
     * @return
     * @throws BusinessException
     */
    static String getData(String url, Map<String, String> params) throws BusinessException {
        if (params != null && !params.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            sb.append(url).append("?");

            for (Entry<String, String> entry : params.entrySet()) {
                if (entry.getValue() != null) {
                    sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            sb.deleteCharAt(sb.lastIndexOf("&"));
            url = sb.toString();
        }
        return getData(url);

    }

    /**
     * 使用post方式获取数据
     * 
     * @param url
     * @param params
     * @return
     * @throws BusinessException
     */
    static String postData(String url, Map<String, String> params) throws BusinessException {
        try {
            if (url != null && params != null) {
                LogUtils.i("url：", url + ";params:" + params.toString());
            }
            String json = NetUtils.doPostString(url, params);
            LogUtils.i(TAG, "result:" + json);
            return resolverData(json);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(BusinessException.CODE_UNREACH_SERVER);
        }
    }

    /**
     * 自定义json转换
     * 
     * @author 王洁
     * @param <T>
     */
    interface JsonParser<T> {
        T parse(String json);
    }

    public interface BusinessCallback<T> {
        /**
         * 成功时调用
         * 
         * @param t 泛型，按照实际返回结果使用指定对象。如果不需要返回对象则使用Void
         */
        void onSuccess(T t);

        /**
         * 发生异常时调用
         * 
         * @param e
         */
        void onError(BusinessException e);
    }
}
